
import jwt
#导入秘钥


class Myjwt():
    def __init__(self):
        self.SECRET_KEY='asfsf!#$#$@$#^%#$sdfwtwa134342432323fffwf'

    #s生成token
    def jwt_encode(self,data):
        #生成token
        token = jwt.encode(data,self.SECRET_KEY,algorithm='HS256')
        return  str(token,encoding='utf8')

    #解码
    def jwt_decode(self,token):

        try:
            #解密
            data = jwt.decode(token,self.SECRET_KEY,algorithms='HS256')
            #重新加密生成新的token
            newtoken = jwt.encode(data,self.SECRET_KEY,algorithms='HS256') 
            return  str(newtoken,encoding='utf8')
        except:
            return None



    #对比
    def check(self,token,newtoken):
        
        #新旧token对比
        if token==newtoken:
            return True
        return False

myjwt = Myjwt()
# token = myjwt.jwt_encode({'data':{'name':'sdf'}})

# newtoken = myjwt.jwt_decode('sdf')
# flag = myjwt.check(token,newtoken)
# print(flag)


